package controllers

import (
	"ABlog/util"
	"fmt"
	"github.com/astaxie/beego"
)

type ApiUploadFileController struct {
	beego.Controller
}

const editormd_image_file string = "editormd-image-file"

func (c *ApiUploadFileController) Post() {
	f, _, err := c.GetFile(editormd_image_file)
	defer f.Close()
	if nil == err {
		fileName := util.GenImageId()
		filePath := fmt.Sprintf("./static/article/assets/%s", fileName)
		err := c.SaveToFile(editormd_image_file, filePath)
		if nil == err {
			c.Data["json"] = map[string]interface{}{
				"success": 1,
				"message": "OK",
				"url":     "/static/article/assets/" + fileName,
			}
		} else {
			c.Data["json"] = map[string]interface{}{
				"success": 0,
				"message": err.Error(),
			}
		}
	} else {
		c.Data["json"] = map[string]interface{}{
			"success": 0,
			"message": err.Error(),
		}
	}
	c.ServeJSON()
}
